import InteractiveBase from "../../../LevelCommon/InteractiveBase";
import PasswordBox from "./PasswordBox";
import Sound from "../../../../constant/SoundNameConstant";
import { utils } from "../../../../utils/CommonUtil";

export default class PasswordBoxKey extends InteractiveBase {
    /** 每次旋转的角度*/
    public rotateAngle: number;
    /** 当前的旋转索引*/
    public curRotateIndex: number = 0;
    /** 旋转索引总数*/
    private rotateTotalIndex: number;

    private curPasswordBox: PasswordBox;

    /** 是否不是tween动画*/
    public isNoTween: boolean = false;
    /** 是x轴*/
    public isX: boolean = false;
    /** 是y轴*/
    public isY: boolean = false;

    private isRotateOver = true;

    private mowner: Laya.Sprite3D;

    private childAtParentIndex = 0;

    onStart() {
        this.mowner = this.owner as Laya.Sprite3D;
        this.curPasswordBox = this.owner.parent.parent.getComponent(PasswordBox);
        this.rotateTotalIndex = Math.floor(360 / Math.abs(this.rotateAngle)) - 1;

        //获取PasswordBox下的所有PasswordBoxKey
        let allBrotherPasswordBoxKey = new Array<PasswordBoxKey>();
        utils.CommonUtils.GetComponentsInChildren<PasswordBoxKey>(this.curPasswordBox.owner as Laya.Sprite3D, PasswordBoxKey, allBrotherPasswordBoxKey);
        for (let index = 0; index < allBrotherPasswordBoxKey.length; index++) {
            if (this.owner.name == allBrotherPasswordBoxKey[index].owner.name) {
                this.childAtParentIndex = index;
                break;
            }
        }
    }

    protected InteractiveLogic(): boolean {
        /** 如果已经打开了 直接返回 */
        if (this.curPasswordBox.isOpen) {
            return false;
        }
        if (!this.isNoTween) {
            if (!this.isRotateOver) {
                return false;
            }
            /** 当前的旋转角度*/
            let curRotateAngle: number = 0;
            let tempLocalEulerAngles = this.mowner.transform.localRotationEuler.clone() as Laya.Vector3;
            if (!this.isX && !this.isY) {
                curRotateAngle = tempLocalEulerAngles.z;
                this.mowner.transform.localRotationEuler = new Laya.Vector3(0, 0, curRotateAngle);
            }
            else {
                if (this.isX) {
                    curRotateAngle = tempLocalEulerAngles.x;
                    this.mowner.transform.localRotationEuler = new Laya.Vector3(curRotateAngle, 0, 0);
                }
                if (this.isY) {
                    curRotateAngle = tempLocalEulerAngles.y;
                    this.mowner.transform.localRotationEuler = new Laya.Vector3(0, curRotateAngle, 0);
                }
            }
            //这边改成了+= 原来是-=
            curRotateAngle += this.rotateAngle;
            curRotateAngle = curRotateAngle % 360;
            let targetAngle: Laya.Vector3 = new Laya.Vector3(0, 0, 0);
            if (!this.isX && !this.isY) {
               
                targetAngle = new Laya.Vector3(0, 0, curRotateAngle);
            }
            else {
            
                if (this.isX) {
                    targetAngle = new Laya.Vector3(curRotateAngle, 0, 0);
                }
                if (this.isY) {
                    targetAngle = new Laya.Vector3(0, curRotateAngle, 0);
                }
            }
            window.gameFacade.SoundMng.playSound(Sound.soundClipArr.physPassLock);

            this.isRotateOver = false;
            utils.CommonUtils.DORotateTween(
                this.mowner.transform,
                this.mowner.transform.localRotationEuler
                , targetAngle, 300, Laya.Handler.create(this, () => {
                    this.curPasswordBox.CheckPassWord();
                    this.isRotateOver = true;

                }), true, false, false)

            /** 增加当前的索引*/
            this.curRotateIndex = (this.curRotateIndex + 1) % (this.rotateTotalIndex + 1);

            this.curPasswordBox.ChildPassBoxKeyCurIndex[this.childAtParentIndex] = this.curRotateIndex;
        }
        /** 不用tween*/
        else {
            if (!this.isX && !this.isY) {
                this.mowner.transform.rotate
                /** 直接旋转*/
                this.mowner.transform.rotate(new Laya.Vector3(0, 0, -this.rotateAngle), true, false);
            }
            else {
                if (this.isX) {
                    /** 直接旋转*/
                    this.mowner.transform.rotate(new Laya.Vector3(this.rotateAngle, 0, 0), true, false);
                }
                if (this.isY) {
                    this.mowner.transform.rotate(new Laya.Vector3(0, -this.rotateAngle, 0), true, false);
                }
            }
            window.gameFacade.SoundMng.playSound(Sound.soundClipArr.physPassLock);
            /** 增加当前的索引*/
            this.curRotateIndex = (this.curRotateIndex + 1) % (this.rotateTotalIndex + 1);
            this.curPasswordBox.ChildPassBoxKeyCurIndex[this.childAtParentIndex] = this.curRotateIndex;
            this.curPasswordBox.CheckPassWord();
        }
        return false;
    }
}